package com.myweb.demo.controller;

import com.myweb.demo.entity.Plate;
import com.myweb.demo.entity.Posts;
import com.myweb.demo.entity.User;
import com.myweb.demo.repository.PlateRepository;
import com.myweb.demo.repository.PostsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PlateController {
    @Autowired
    PlateRepository plateRepository;
    @Autowired
    PostsRepository postsRepository;

    @GetMapping("/showplate/{plateid}")
    public ModelAndView show_posts(HttpServletRequest request, @PathVariable(name = "plateid")Long id, Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer userstatu = (Integer) session.getAttribute("userstatu");
        if (userstatu == null){
            userstatu = 0;
        }
        model.addAttribute("user",user);
        model.addAttribute("userstatu",userstatu);
        //获取板块信息
        List<Plate> plateList = plateRepository.findAll();
        model.addAttribute("platelist",plateList);
        Plate plate = plateRepository.getOnePlateByPlateId(id);
        model.addAttribute("plate",plate);
        //获取板块内帖子信息
        List<Posts> postsList = postsRepository.getPostsByPlate(plate);
        model.addAttribute("postslist",postsList);
        return new ModelAndView("show_plate");
    }
}
